<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品信息" prop="info">
      <el-input v-model="dataForm.info" placeholder="商品信息"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="image">
      <single-upload v-model="dataForm.image"></single-upload>
    </el-form-item>
    <el-form-item label="商品类型" prop="productTypeId">
<category-cascader :catelogPath.sync="catelogPath" @update="update"></category-cascader>
    </el-form-item>
    <el-form-item label="商家id" prop="businessId">
      <el-input v-model="dataForm.businessId" placeholder="商家id" disabled></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

  import SingleUpload from "../upload/singleUpload";
  import CategoryCascader from '../components/category-cascader'
  export default {
    components: {CategoryCascader, SingleUpload},
    data () {
      return {
        catelogPath: [],
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          price: '',
          info: '',
          image: '',
          productTypeId: '',
          businessId: sessionStorage.getItem("id")
        },
        dataRule: {
          name: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          info: [
            { required: true, message: '商品信息不能为空', trigger: 'blur' }
          ],
          image: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          productTypeId: [
            { required: true, message: '商品类型不能为空', trigger: 'blur' }
          ],
          businessId: [
            { required: true, message: '商家id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.gettree()
    },
    methods: {
        update(v)
      {

        this.dataForm.productTypeId=v;
      },
      gettree()
      {
        this.$http({
          url: this.$http.adornUrl(`/yunxi/pmscategory/tree`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(res=>{
          this.catelogPath=res.data.data;
                }

        )
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/yunxi/tproduct/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.tProduct.name
                this.dataForm.price = data.tProduct.price
                this.dataForm.info = data.tProduct.info
                this.dataForm.image = data.tProduct.image
                this.dataForm.productTypeId = data.tProduct.productTypeId
                this.dataForm.businessId = data.tProduct.businessId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/yunxi/tproduct/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'price': this.dataForm.price,
                'info': this.dataForm.info,
                'image': this.dataForm.image,
                'productTypeId': this.dataForm.productTypeId,
                'businessId': this.dataForm.businessId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
